<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>自动计算font-size</title>

  <style>
        /* 重置默认样式 */
        * {
            margin: 0;
            padding: 0;
        }

  </style>

  <script>
    (function () {
      function calc() {
        const w = document.documentElement.clientWidth;
        const f = document.documentElement.clientWidth;
        // 1rem = 75px
        document.documentElement.style.fontSize = 75 * (f / 750) + 'px';
        console.log(w);
      }
      // 页面加载时立即执行calc函数
      calc();
      // 监听窗口大小变化事件，当设备尺寸改变时重新计算font-size
      window.onresize = function () {
        calc();
      };
    })();
  </script>

</head>

<body>
  <!-- 页面内容 -->
   <div style="width: 200px;height: 200px;background-color: green;font-size: 20px;color: white;display: inline-block;"></div>
   <div style="width: 300px;height: 200px;background-color: pink;display: inline-block;"></div>
</body>

</html>
